//
//  GetAddressRequest.m
//  fashionbuyer
//
//  Created by ale tan on 2018/1/17.
//  Copyright © 2018年 ale tan. All rights reserved.
//

#import "GetAddressRequest.h"
#import "FFTools.h"

@implementation GetAddressRequest

- (id)init{
    self = [super init];
    if (self) {
        self.getAddressListUrl = @"platform/v1/addresses";
        self.createAddressUrl = @"platform/v1/address/add";
        self.editAddressUrl = @"platform/v1/address/modify";
    }
    return self;
}

//获取列表
- (void)getAddressListWithComplete:(RequestResult)requestResult{
    self.requestCallBack = requestResult;
    
    [ServiceManager postTrans:self.getAddressListUrl withRequest:self.parameters success:^(NSDictionary *dictResponse, BOOL result) {
        
        if (result) {
            NSArray *array = dictResponse[@"data"][@"addresses"];
            NSMutableArray *addresses = [NSMutableArray array];
            for (NSDictionary *dic in array) {
                FBAddressModel *model  = [[FBAddressModel alloc]initWithDictionary:dic];
                [addresses addObject:model];
            }
            
            self.requestCallBack(addresses, YES);
        }
        else{
            [FFTipsView showTips:dictResponse[@"toast"]];
            self.requestCallBack(dictResponse,NO);
        }
        
    } failure:^(NSError *error) {
        [FFTipsView showTips:@"网络连接失败"];
        self.requestCallBack(nil,NO);
    }];
}

//新建地址
- (void)addAddressWithModel:(FBAddressModel *)model Complete:(RequestResult)requestResult{
    
    self.requestCallBack = requestResult;
    
    [self.parameters setValue:model.address forKey:@"address"];
    if (model.isDefault) {
        [self.parameters setValue:[NSString stringWithFormat:@"%@",model.isDefault] forKey:@"isDefault"];
    }
    [self.parameters setValue:model.consigneeName forKey:@"consigneeName"];
    [self.parameters setValue:model.consigneePhone forKey:@"consigneePhone"];
    
    [self.parameters setValue:model.addressCountry forKey:@"addressCountry"];
    [self.parameters setValue:model.countryCode forKey:@"countryCode"];
    
    if ([model.countryCode isEqual:@"+86"]) {
        [self.parameters setValue:model.consigneeIdcard forKey:@"consigneeIdcard"];
    }
    
    [ServiceManager postTrans:self.createAddressUrl withRequest:self.parameters success:^(NSDictionary *dictResponse, BOOL result) {
        
        if (result) {
            NSDictionary *addressDic = dictResponse[@"data"];
            FBAddressModel *model  = [[FBAddressModel alloc]initWithDictionary:addressDic];
            self.requestCallBack(model, YES);
        }
        else{
            [FFTipsView showTips:dictResponse[@"toast"]];
            self.requestCallBack(dictResponse,NO);
        }
        
    } failure:^(NSError *error) {
        [FFTipsView showTips:@"网络连接失败"];
        self.requestCallBack(nil,NO);
    }];

}

//编辑地址
- (void)editAddressWithModel:(FBAddressModel *)model Complete:(RequestResult)requestResult{
    self.requestCallBack = requestResult;
    
    [self.parameters setValue:model.addressId forKey:@"addressId"];
    
    [self.parameters setValue:model.address forKey:@"address"];
    if (model.isDefault) {
        [self.parameters setValue:[NSString stringWithFormat:@"%@",model.isDefault] forKey:@"isDefault"];
    }
    [self.parameters setValue:model.consigneeName forKey:@"consigneeName"];
    [self.parameters setValue:model.consigneePhone forKey:@"consigneePhone"];
    
    [self.parameters setValue:model.addressCountry forKey:@"addressCountry"];
    [self.parameters setValue:model.countryCode forKey:@"countryCode"];
    
    if ([model.countryCode isEqual:@"+86"]) {
        [self.parameters setValue:model.consigneeIdcard forKey:@"consigneeIdcard"];
    }
    
    [ServiceManager postTrans:self.editAddressUrl withRequest:self.parameters success:^(NSDictionary *dictResponse, BOOL result) {
        
        if (result) {
            self.requestCallBack(model, YES);
        }
        else{
            [FFTipsView showTips:dictResponse[@"toast"]];
            self.requestCallBack(dictResponse,NO);
        }
        
    } failure:^(NSError *error) {
        [FFTipsView showTips:@"网络连接失败"];
        self.requestCallBack(nil,NO);
    }];
}

#pragma mark -Tool-
//判断是否是可用的身份证
+ (BOOL)isValidIDCard:(NSString *)idCard{
    BOOL flag;
    if (idCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:idCard];
}

@end
